﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FF.Entity;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Data;

namespace FF.Method
{
    class CustomerMethod
    {
        public static int delete(string id)
        {
            try
            {
                string sql = "delete from ff_customer where customer_id='" + id + "'";
                return SQLiteHelper.ExecuteSql(sql);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return 0;
            }

        }

        public static int add(Customer entity)
        {
            string sql = "insert into ff_customer(name,type,discount,phone,tel,address,remark,user_id,user_name)  values('";
           // sql += entity.customerId + "','";
            sql += entity.name + "','";
            sql += entity.type + "','";
            sql += entity.discount + "','";
            sql += entity.phone + "','";
            sql += entity.tel + "','";
            sql += entity.address + "','";
            sql += entity.remark + "','";
            sql += entity.userId + "','";

            sql += entity.userName + "')";
            try
            {
                return SQLiteHelper.ExecuteSql(sql);
            }
            catch (Exception ex)
            {

                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return 0;
            }
        }

        public static int update(Customer entity)
        {
            string sql = "update ff_customer set name='" + entity.name + "',";
            sql += "type='" + entity.type + "',";
            sql += "discount='" + entity.discount + "',";
            sql += "phone='" + entity.phone + "',";
            sql += "tel='" + entity.tel + "',";
            sql += "address='" + entity.address + "',";
            sql += "remark='" + entity.remark + "',";
            sql += "user_id='" + entity.userId + "',";
            //sql += "type='" + entity.type + "',";
            sql += "user_name='" + entity.userName + "' where customer_id='" + entity.customerId + "'";
            try
            {
                return SQLiteHelper.ExecuteSql(sql);
            }
            catch (Exception ex)
            {

                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return 0;
            }
        }
        public static DataSet queryByName(string keyword)
        {
            string sql = "select  a.customer_id,a.name,b.name as type,a.discount,a.phone,a.tel,a.address,a.remark,a.user_name from ff_customer a ,ff_customer_type b where a.type = b.type and a.name like '%" + keyword + "%'";
            try
            {
                return SQLiteHelper.Query(sql);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        public static DataSet queryById(string keyword)
        {
            string sql = "select  a.customer_id,a.name,b.name as type,a.discount,a.phone,a.tel,a.address,a.remark,a.user_name from ff_customer a ,ff_customer_type b where a.type = b.type and a.customer_id like '%" + keyword + "%'";
            try
            {
                return SQLiteHelper.Query(sql);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }


        internal static Customer getEntityById(string id)
        {
            Customer customer = null;
            try
            {
                SQLiteDataReader reader = SQLiteHelper.ExecuteReader("select  a.customer_id,a.name,a.type,a.discount,a.phone,a.tel,a.address,a.remark,a.user_id,a.user_name from ff_customer a ,ff_customer_type b where a.type = b.type and  a.customer_id='" + id + "'");
                if (reader.HasRows)
                {
                    reader.Read();
                    customer = new Customer();
                    customer.customerId = reader.GetInt32(0).ToString();
                    customer.name = reader.GetString(1);
                    customer.type = reader.GetString(2);
                    customer.discount = reader.GetString(3);
                    customer.phone = reader.GetString(4);
                    customer.tel = reader.GetString(5);
                    customer.address = reader.GetString(6);
                    customer.remark = reader.GetString(7);
                    customer.userId = reader.GetString(8);
                    customer.userName = reader.GetString(9);
                   
                }
                reader.Close();
                return customer;
            }
            catch (Exception ex)
            {
                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        public static void fillTree(object objTreeView, object obimage)
        {
            if (objTreeView.GetType().ToString() == "System.Windows.Forms.TreeView")
            {
                System.Windows.Forms.ImageList imlist = (System.Windows.Forms.ImageList)obimage;
                System.Windows.Forms.TreeView TV = (System.Windows.Forms.TreeView)objTreeView;
                TV.Nodes.Clear();
                TV.ImageList = imlist;
                try
                {
                    SQLiteDataReader readerType = SQLiteHelper.ExecuteReader("select * from ff_customer_type");
                    SQLiteDataReader rederUser;
                    if (readerType.HasRows)
                    {
                        System.Windows.Forms.TreeNode TN;
                        while (readerType.Read())
                        {
                            TN = TV.Nodes.Add(readerType.GetString(0), readerType.GetString(1), 1, 0);
                            rederUser = SQLiteHelper.ExecuteReader("select customer_id, name from ff_customer where type='" + readerType.GetString(0) + "'");
                            if (rederUser.HasRows)
                            {
                                while (rederUser.Read())
                                {
                                    //TreeNode newNode12 = new TreeNode(rederUser.GetString(0), 1, 0);
                                    //newNode12.Nodes.Add("A", qlddr[4].ToString(), 0, 1);
                                    //TN.Nodes.Add(newNode12);
                                    TN.Nodes.Add(rederUser.GetInt32(0).ToString(), rederUser.GetString(1), 1, 0);
                                }
                            }
                            rederUser.Close();

                        }
                        TV.ExpandAll();

                    }
                    readerType.Close();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return ;
                }

            }
        }

        internal static string getDiscountById(string id)
        {
            string discount = null;
            try
            {
                SQLiteDataReader reader = SQLiteHelper.ExecuteReader("select  a.customer_id,a.name,a.type,a.discount,a.phone,a.tel,a.address,a.remark,a.user_id,a.user_name from ff_customer a ,ff_customer_type b where a.type = b.type and  a.customer_id='" + id + "'");
                if (reader.HasRows)
                {
                    reader.Read();
                    discount = reader.GetString(3);
                }
                reader.Close();
                return discount;
            }
            catch (Exception ex)
            {
                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return "1";
            }
        }
    }
}
